// Read a page's GET URL variables and return them as an associative array.
function getUrlParams()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }

    return vars;
}

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

function toggleSpinner(control)
{

	$j("#spinner").remove();
	var element = $j("#attendee_t");
	var offset = element.offset();
	var width = element.width()+2;
	var height = element.height()+2;
	element.append('<div id="spinner"><img src="http://lorus.org/dev/mybb/images/spinner_big.gif" style="position: absolute; top: 50%; left: 50%; margin-left: -35px; margin-top: -35px;"></div>');
	$j("#spinner").css({
		"display" : "none",
		"background-color" : "white",
		"opacity" : "0.8",
		"position" : "absolute",
		"top" : offset.top,
		"left" : offset.left,
		"height" : height,
		"width" : width
	});

	if (control == "on") {
		$j("#spinner").show();
	} 
	if (control == "off") {
		$j("#spinner").hide()
		$j("#spinner").remove();
	}

}

//tooltip function
this.tooltip = function(){	
	/* CONFIG */		
		xOffset = 10;
		yOffset = 20;
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$j("img.tooltip").live({
		mouseover: function(e){	
			element = $j(this);
			element.prop("t", element.prop("title") )
			element.prop("title", "");								  
			$j("body").append("<p id='tooltip'>"+ element.prop("t") +"</p>");
			$j("#tooltip")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px")
				.fadeIn("fast");		
    },
		mouseout: function(){
			element = $j(this);
			element.prop("title", element.prop("t") );
			element.prop("t", "");		
			$j("#tooltip").remove();
    },
		mousemove: function(e){
			$j("#tooltip")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px");
		}
	});				
};

function selectItem(li) {
	return false;
}
	
function formatItem(row) {
	return row[0] + "<br><i>" + row[1] + "</i>";
}

function hideMore() {
	$j( "#more_accepted" ).hide();
	$j( "#more_refused" ).hide();
	$j( "#more_invited" ).hide();
	$j( "#showmore_accepted" ).show();
	$j( "#showmore_refused" ).show();
	$j( "#showmore_invited" ).show();
}
	
$j(document).ready(function(){

	//run tooltips
	tooltip();
	
	hideMore();

	var url = window.location;
	var url_params = getUrlParams();
	var id= "";
	if (typeof url_params['eid'] != 'undefined') {
		id = "&eid="+url_params['eid'];
	}
	if (typeof url_params['tid'] != 'undefined') {
		id = "&tid="+url_params['tid'];
	}
	
	var xmlhttp = "xmlhttp.php?plugin=event_attendees";

	$j( "button" ).button();

	function split( val ) {
		return val.split( /,\s*/ );
	}
	function extractLast( term ) {
		return split( term ).pop();
	}

	$j( "#einvite" )
		// don't navigate away from the field on tab when selecting an item
		.bind( "keydown", function( event ) {
			if ( event.keyCode === $j.ui.keyCode.TAB &&
					$j( this ).data( "autocomplete" ).menu.active ) {
				event.preventDefault();
			}
		})
		.autocomplete({
			source: function( request, response ) {
				$j.getJSON( xmlhttp + "&autocomplete=user", {
					term: extractLast( request.term )
				}, response );
			},
			search: function() {
				// custom minLength
				var term = extractLast( this.value );
				if ( term.length < 2 ) {
					return false;
				}
			},
			focus: function() {
				// prevent value inserted on focus
				return false;
			},
			select: function( event, ui ) {
				var terms = split( this.value );
				// remove the current input
				terms.pop();
				// add the selected item
				terms.push( ui.item.value );
				// add placeholder to get the comma-and-space at the end
				terms.push( "" );
				this.value = terms.join( ", " );
				return false;
			}
	});
	
	$j( "#einvite_group" )
		// don't navigate away from the field on tab when selecting an item
		.bind( "keydown", function( event ) {
			if ( event.keyCode === $j.ui.keyCode.TAB &&
					$j( this ).data( "autocomplete" ).menu.active ) {
				event.preventDefault();
			}
		})
		.autocomplete({
			source: function( request, response ) {
				$j.getJSON( xmlhttp + "&autocomplete=group", {
					term: extractLast( request.term )
				}, response );
			},
			search: function() {
				// custom minLength
				var term = extractLast( this.value );
				if ( term.length < 2 ) {
					return false;
				}
			},
			focus: function() {
				// prevent value inserted on focus
				return false;
			},
			select: function( event, ui ) {
				var terms = split( this.value );
				// remove the current input
				terms.pop();
				// add the selected item
				terms.push( ui.item.value );
				// add placeholder to get the comma-and-space at the end
				terms.push( "" );
				this.value = terms.join( ", " );
				return false;
			}
	});

	
	self = $j("#self").parent().parent().prop("id");
	self_more = $j("#self").parent().prop("id");
	
	if (self == "accepted" || self_more == "more_accpeted"){
		$j("#accept").button('disable');
		$j("#comment").hide();
	}
	if (self == "refused" || self_more == "more_refused"){
		$j("#refuse").button('disable');
		  $j("#comment").hide();
	}
	$j("#refuse").click(function(){
		toggleSpinner("on");
		$j.ajax({
			type: "GET",
			url: xmlhttp,
			dataType: "json",
			data: "action=change_status&status=refuse"+id+"&comment="+$j("#ecomment").val(),
			success: function(data){
				toggleSpinner("off");
				$j("#status").html(data.status);
				$j("#status").attr("class", data.type);
				$j("#status").show();
				$j("#attendee_list").html(data.html);
				$j("#accept").button('enable');
				$j("#refuse").button('disable');
				$j("#maybe").show();
				$j("#percent_select").hide();
				$j( "button" ).button();
				$j( "button" ).removeClass("ui-state-focus");
				$j("#ecomment, #comment").hide();
				hideMore();
			}
		});
	});
	
	$j("#accept").click(function(){
		toggleSpinner("on");
		$j.ajax({
			type: "GET",
			url: xmlhttp,
			dataType: "json",
			data: "action=change_status&status=accept"+id+"&comment="+$j("#ecomment").val(),
			success: function(data){
				toggleSpinner("off");
				$j("#status").html(data.status);
				$j("#status").attr("class", data.type);
				$j("#status").show();
				$j("#attendee_list").html(data.html);
				$j("#accept").button('disable');
				$j("#refuse").button('enable');
				$j("#maybe").show();
				$j("#percent_select").hide();
				$j( "button" ).button();
				$j( "button" ).removeClass("ui-state-focus");
				$j("#ecomment, #comment").hide();
				hideMore();
			}
		});
	});
	
	$j("#maybe").click(function(){
		this.hide();
		$j("#percent_select").show();
	});	
	
	$j("#percent_select").change(function(){
		percent = $j('#percent_select option:selected').val();
		toggleSpinner("on");
		$j.ajax({
			type: "GET",
			url: xmlhttp,
			dataType: "json",
			data: "action=change_status&status=maybe"+id+"&comment="+$j("#ecomment").val()+"&percent="+percent,
			success: function(data){
				toggleSpinner("off");
				$j("#status").html(data.status);
				$j("#status").attr("class", data.type);
				$j("#status").show();
				$j("#attendee_list").html(data.html);
				$j("#accept, #refuse").button('enable');
				$j( "button" ).button();
				$j( "button" ).removeClass("ui-state-focus");
				$j("#ecomment, #comment").hide();
				hideMore();
			}
		});
	});
	
	// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
	$j( "#dialog:ui-dialog" ).dialog( "destroy" );

	$j( "#dialog-form-invite-user" ).dialog({
		autoOpen: false,
		resizable: false,
		draggable: false,
		modal: true,
		buttons: {
			"OK" : function() {
				$j("#dialog-form-invite-user").dialog( "close" );
				toggleSpinner("on");
				$j.ajax({
					type: "GET",
					url: xmlhttp,
					dataType: "json",
					data: "action=invite&invite="+$j("#einvite").prop("value")+id,
					success: function(data){
						toggleSpinner("off");
						$j("#status").html(data.status);
						$j("#status").attr("class", data.type);
						$j("#status").show();
						$j("#attendee_list").html(data.html);
						$j("#einvite").prop("value", "");
						$j( "button" ).button();
						hideMore();
					}
				});
			},
			Cancel: function() {
				$j( this ).dialog( "close" );
			}
		}
	});

	$j( "#bdialog_invite_user" ).click(function() {
			$j( "#dialog-form-invite-user" ).dialog( "open" );
	});
	
	$j( "#dialog-form-invite-group" ).dialog({
		autoOpen: false,
		resizable: false,
		draggable: false,
		modal: true,
		buttons: {
			"OK" : function() {
				$j("#dialog-form-invite-group").dialog( "close" );
				toggleSpinner("on");
				$j.ajax({
					type: "GET",
					url: xmlhttp,
					dataType: "json",
					data: "action=invite_grp&invite="+$j("#einvite_group").prop("value")+id,
					success: function(data){
						toggleSpinner("off");
						$j("#status").html(data.status);
						$j("#status").attr("class", data.type);
						$j("#status").show();
						$j("#attendee_list").html(data.html);
						$j("#einvite_group").prop("value", "")
						$j( "button" ).button();
						hideMore();
					}
				});
			},
			Cancel: function() {
				$j( this ).dialog( "close" );
			}
		}
	});

	$j( "#bdialog_invite_group" ).click(function() {
			$j( "#dialog-form-invite-group" ).dialog( "open" );
	});
	
	$j( "#self_cancel" ).live({
		click: function(e) {
			e.stopPropagation();
			$j( "#self" ).addClass("tooltip");
			$j( "#self_text, #self_editicon").show();
			$j( "#self_ok, #self_input, #self_cancel").hide();
		}
	});
	
	$j( "#self_ok" ).live({
		click: function(e) {
			e.stopPropagation();
			toggleSpinner("on");
			$j.ajax({
				type: "GET",
				url: xmlhttp,
				dataType: "json",
				data: "action=update_comment"+id+"&comment="+$j("#self_input").val(),
				success: function(data){
					toggleSpinner("off");
					$j("#attendee_list").html(data.html);
					$j( "button" ).button();
					$j("#ecomment, #comment").hide();
					$j("#ecomment").prop("value", "");
					hideMore();
				}
			});
		}
	});
	
	$j( "#self_editicon" ).live({
		click: function() {
			$j( "#self" ).removeClass("tooltip");
			$j("#tooltip").remove();
			$j( "#self_text").hide();
			$j( "#self_ok, #self_cancel, #self_input").show();
			$j( "#self_editicon").hide();
		}
	});
	
	$j("#comment").click(function(e){
		e.preventDefault();
		$j(this).hide();
		$j("#ecomment").show();
	});
	
	$j("#showmore_refused > a").live({
		click: function(e){
			e.preventDefault();
			$j("#showmore_refused").hide();
			$j("#more_refused").show();
		}
	});
	
	$j("#showmore_accepted > a").live({
		click: function(e){
			e.preventDefault();
			$j("#showmore_accepted").hide();
			$j("#more_accepted").show();
		}
	});
	
	$j("#showmore_invited > a").live({
		click: function(e){
			e.preventDefault();
			$j("#showmore_invited").hide();
			$j("#more_invited").show();
		}
	});
	
	$j("#accepted li").draggable({
		revert: true,
		revertDuration: 0
	});
	
	$j("#set_maybe li").draggable({
		revert: true,
		revertDuration: 0
	});
	
	$j("#refused li").draggable({
		revert: true,
		revertDuration: 0
	});
	
	$j("#invited li").draggable({
		revert: true,
		revertDuration: 0
	});
	
	$j( "#accepted" ).droppable({
		drop: function( event, ui ) {
			ui.helper.appendTo( $j("#accepted ul") );
			$j( "#accepted" ).css("border", "");
		},
		over: function() {
			$j( "#accepted" ).css("border", "1px dashed red");
		},
		out: function() {
			$j( "#accepted" ).css("border", "");
		}
	});
	
	$j( "#set_maybe" ).droppable({
		drop: function( event, ui ) {
			ui.helper.appendTo( $j("#set_maybe ul") );
			$j( "#set_maybe" ).css("border", "");
		},
		over: function() {
			$j( "#set_maybe" ).css("border", "1px dashed red");
		},
		out: function() {
			$j( "#set_maybe" ).css("border", "");
		}
	});
	
	$j( "#refused" ).droppable({
		drop: function( event, ui ) {
			ui.helper.appendTo( $j("#refused ul") );
			$j( "#refused" ).css("border", "");
		},
		over: function() {
			$j( "#refused" ).css("border", "1px dashed red");
		},
		out: function() {
			$j( "#refused" ).css("border", "");
		}
	});
	
	$j( "#invited" ).droppable({
		drop: function( event, ui ) {
			//ui.helper.appendTo( $j("#invited ul") );
			$j( "#invited" ).css("border", "");
		},
		over: function() {
			$j( "#invited" ).css("border", "1px dashed red");
		},
		out: function() {
			$j( "#invited" ).css("border", "");
		}
	});
	
	
});

